/**
 * (c) 2012 Samuel Nelson. All rights reserved
 */

// Our browser specific object
var helpplz_browser = new Array();

// Loads the stored list of accounts and passes the data to the given callback
helpplz_browser.load = function(callback) {
    self.port.on('loadRet', function(payload) {
		$('head').append('<style>' + payload.css + '</style>');
		callback(payload.storage);
	});
    self.port.emit('load', {});
}

// Commits all changes to the list of accounts to local storage
helpplz_browser.save = function(lstore) {
    self.port.emit('saveStorage', lstore);
}

// Fetches the JSON object at the given url and passes the result to the
// given callback on success. If used to fetch JSON from a different domain,
// server must support CORS
helpplz_browser.getJSON = function(url, callback) {
    var xmlhttp;
    xmlhttp=new XMLHttpRequest();
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            callback($.parseJSON(xmlhttp.responseText));
        }
    }
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
}